package com.newtouch.avmarketing.mgr.modules.avmarkrting.api;

import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.Branch;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.DoubleConfiguration;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.SaleChannel;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.DoubleCondition;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.DoubleConfigurationReq;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.Result;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.model.ResultCodes;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.BranchService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.DoubleConfigurationService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.SaleChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "双录配置API", tags = "02 双录管理：双录配置")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/doubleConfiguration")
public class DoubleConfigurationApi {

    @Autowired
    private DoubleConfigurationService doubleConfigurationService;
    @Autowired
    private BranchService branchService;
    @Autowired
    private SaleChannelService saleChannelService;

    @ApiOperation("查询渠道信息")
    @GetMapping("/findAllSales")
    public Result findAllSales(){
        List<SaleChannel> list = saleChannelService.findAllChannel();
        if (list.size()!=0){
            return Result.of(ResultCodes.SUCCESS_CODE,"查询成功",list);
        }else {
            log.error("查询失败");
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败");
    }

    @ApiOperation("查询分公司信息")
    @GetMapping("/findByLevel")
    public Result findByLevel(){
        List<Branch> list = branchService.findByLevel();
        if (list.size()!=0){
            return Result.of(ResultCodes.SUCCESS_CODE,"查询成功",list);
        }else {
            log.error("查询失败");
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败");
    }

    @ApiOperation("条件查询双录列表")
    @PostMapping(value = "/findByStepNameContaining")
    public Result findByStepNameContaining(@RequestBody DoubleCondition doubleCondition){
        try{
            Page<DoubleConfiguration> content = doubleConfigurationService.findByStepNameContaining(doubleCondition);
            if( null != content ){
                return Result.of(ResultCodes.SUCCESS_CODE,"查询成功！",content);
            }
        }catch (Exception e){
        }
        return Result.of(ResultCodes.ERROR_CODE,"查询失败！");
    }
    @ApiOperation("新增双录步骤")
    @PostMapping("/saveConfiguration")
    public Result saveConfiguration(@RequestBody DoubleConfigurationReq doubleConfigurationReq){
        try {
            int num = doubleConfigurationService.saveConfiguration(doubleConfigurationReq);
            if( num == 1 ){
                return Result.of(ResultCodes.SUCCESS_CODE,"保存成功！");
            }
        }catch (Exception e){
        }
        return Result.of(ResultCodes.ERROR_CODE,"保存失败！");
    }

    @ApiOperation("修改双录步骤")
    @PostMapping(value = "/updateConfigurationById")
    public Result updateConfigurationById(@ApiParam("DoubleConfigurationReq") @RequestBody DoubleConfigurationReq doubleConfigurationReq) {
        try {
            int nu = doubleConfigurationService.updateConfigurationById(doubleConfigurationReq);
            if(nu==1){
                return Result.of(ResultCodes.SUCCESS_CODE,"更新成功！");
            }
        }catch (Exception e){
        }
        return Result.of(ResultCodes.ERROR_CODE,"更新失败！");
    }
    @ApiOperation("删除双录步骤")
    @PostMapping(value = "/deleteConfigurationById/{stepId}")
    public Result deleteConfigurationById(@PathVariable Long stepId){
        try {
            int i = doubleConfigurationService.deleteConfiguration(stepId);
            if(i==1){
                return Result.of(ResultCodes.SUCCESS_CODE,"删除成功！");
            }
        }catch (Exception e){
        }
        return Result.of(ResultCodes.ERROR_CODE,"删除失败！");
    }


}
